package com.zq.it.action.admin;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import com.zq.it.action.BaseAction;
import com.zq.it.pojo.Netcourse;
import com.zq.it.service.admin.NetCourseService;
import com.zq.it.tool.Constant;
import com.zq.it.tool.EtyLsName;
import com.zq.it.tool.FileName;
import com.zq.it.tool.FileType;
import com.zq.it.tool.HeadTitle;

import net.sf.json.JSONArray;

public class NetCourseAction extends BaseAction {
	
	@Resource
	private NetCourseService netCourseService;
	
	private Netcourse netcourse=null;
	private Integer [] netCourseGroupId;
	public void searchNetcListByHQL(){		
		List list=netCourseService.getNetcListByHQL(netcourse);
		uploadList(EtyLsName.NETC_LS.name(),list);
		print(JSONArray.fromObject(list,Constant.getJcf(Float.class)).toString(),"UTF-8","APPLICATION/JSON");
	}
	
	public void deleteNetcGroups(){
		netCourseService.removeAllNetcourse(netCourseGroupId);
		print("已成功删除"+netCourseGroupId.length+"条记录！","UTF-8","text/html");
	}
	
	public void  addNetcPo(){
		  //保存文件路径
		     //String  basePath= getRealPath("/video");   	        
		     int length=getUploadFileName().length();
		     String type=getUploadFileName().substring(length-4,length);  //文件类型
		     String fileName=String.valueOf(System.currentTimeMillis()); //文件名
	         String tmpFile=Constant.STATIC_BASE_PATH+File.separator+"tmpmv"+File.separator+fileName+type;  
	         
	      if(upLoad(getUpload(),tmpFile)){               
	         String netcoursePicPath = Constant.STATIC_BASE_PATH +File.separator+"picture" +File.separator+ fileName+ ".jpg";  //设置上传视频截图的保存路径                   
	         String ffmpegPath =getRealPath("/tool/ffmpeg.exe");// 获取配置的转换工具（ffmpeg.exe）的存放路径	        
	         netcourse.setPicture(fileName+".jpg"); 
	         netcourse.setFlvName(fileName+type);
	         
	        if(netCourseService.addNetcourse(netcourse,ffmpegPath,tmpFile,netcoursePicPath))         
		       print("<script>parent.parent.closeWindow('已上传成功！');</script>","UTF-8","text/html");			        	 
	    }
	}
	
	public void modifyNetcPo(){
		netCourseService.renewalNetcourse(netcourse);
		print("<script>parent.closeWindow('已修改！');</script>","UTF-8","text/html");	
	}
	
	public String getOnlineCoursePage() {
		List types = netCourseService.findNetCourseType();
		getRequest().setAttribute(Constant.NETCOURSE_TYPES, types);
		getRequest().setAttribute(Constant.NETCOURSE, netCourseService.findNetCourseByType(types.get(0).toString()));
		return "onlinecourses";
	}
	
	public void searchNetCourses() throws IOException {
		print_json_utf8(JSONArray.fromObject(netCourseService.findNetCourseByType(type)).toString());
	}
	
	
	public void exportNetcList(){				
		download(netCourseService.exportNetcourseList(HeadTitle.CN_NETCHEAD.getArray(),(List<Netcourse>)(getHttpSession().getAttribute(EtyLsName.NETC_LS.name()))),FileName.NETC_LIST_NAME.toString()+FileType.XLSX);
	}
	
	public Netcourse getNetcourse(){
		return netcourse;
	}
	public void setNetcourse(Netcourse netc){
		netcourse = netc;
	}
	public Integer[] getNetCourseGroupId() {
		return netCourseGroupId;
	}

	public void setNetCourseGroupId(Integer[] netCourseGroupId) {
		this.netCourseGroupId = netCourseGroupId;
	}
	private String type;
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
